/*
 * Vlabs Biological Web Elemments
 *
 * Copyright (c) 2009 Victor de la Torre (vdelatorre@cnio.es)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 * 
 * http://code.google.com/p/bwe/ 
 *
 */
 (function($) {
 
    $.bwe.ihop = $.bwe.ihop || {}; //Add the 'ihop' scope
    
    
	$.bwe.ihop.instance = function(container,label,onclick,functionLabel){
		
		$.bwe.buidElement(container,label,'$.bwe.ihop.'+onclick,functionLabel);
	}
	
 	$.bwe.ihop.getRelatedSymbols = function(id,label){

 		if ($.bwe.animate(id,400,200)){
	 		var serv = new Ihop();
			serv.getRelatedSymbols(function(data){
				table = jQuery('<table cellpadding="5" cellspacing="0"></table>');
				c = 0;
				$.each(data.iHOPrelatedSymbols.iHOPsymbol,function(i,item) {
	
					symbol 		= item.attributes.symbol;
					organism 	= item.attributes.organism;
					name 		= item.attributes.name;
					iclass		= (c%2==0)?'normal':'gray';
					tr = jQuery('<tr><td colspan="2" class="bwe-td-'+iclass+' bwe-h10">'+symbol+'</td></tr><tr><td class="bwe-td-'+iclass+' bwe-h11">'+organism+'</td><td class="bwe-td-'+iclass+' bwe-h11-blue">'+name+'</td></tr>');	
					table.append(tr);
					c++;			
				});
				$("#loading"+id).remove();
				$("#"+id).append(table);
			
			},label);
		}
 	}
 	
 	 $.bwe.ihop.getSymbolInfo = function(id,label){

 		if ($.bwe.animate(id,300,180)){

	 		var serv = new Ihop();
			serv.getSymbolInfo(function(data){
				
				table = jQuery('<table cellpadding="5" cellspacing="0"></table>');
				c = 0;
				$.each(data.iHOPsymbolInfo.iHOPsymbol.dblink,function(i,item) {
	
					dbName			= item.attributes.dbNamespace.replace(/_/g,' ');
					dbId 			= item.attributes.dbId;
					iclass		= (c%2==0)?'normal':'gray';
					tr = jQuery('<tr><td colspan="2" class="bwe-td-'+iclass+' bwe-h11">'+dbName+'</td><td class="bwe-td-'+iclass+' bwe-h11-blue">'+dbId+'</td></tr>');	
					table.append(tr);
					c++;			
				});
				$("#loading"+id).remove();
				$("#"+id).append(table);
			
			},label,'');
		}
 	}
 	
 	$.bwe.ihop.getSymbolInteractions = function(id,label){

 		if ($.bwe.animate(id,700,300)){

	 		var serv = new Ihop();

 			serv.getSymbolInteractions(function(data){
 			
 			xml = Base64.decode(data.response);
 			
 			table = jQuery('<table cellpadding="5" cellspacing="0"></table>');
 			c = 0;
 			$(xml).find("iHOPsentence").each(function(){
 				
 				iclass			= (c%2==0)?'normal':'gray';
 				var sentence 	= $(this);
 				pmid 			= sentence.attr("pmid");
 				journalImpact 	= (sentence.attr("journalImpact"))?sentence.attr("journalImpact"):'Unknown';
 				year 			= sentence.attr("year");
 				text			= sentence.text(); 
 				
 				$(sentence).find("iHOPatom").each(function(){
 					var atom = $(this);
 					evidence = atom.text();
 					var sRegExInput = new RegExp(evidence, "g");
 					text = text.replace(sRegExInput,'<span class="bwe-h11-blue">'+evidence+'</span>');
 				});
				
				tr1 = jQuery('<tr><td colspan="3" class="bwe-td-'+iclass+' bwe-textArial12">'+text+'</td>');
				tr2 = jQuery('<tr><td class="bwe-td-'+iclass+' bwe-h11"><span style="color:#111111;">PMID</span> '+pmid+'</td><td class="bwe-td-'+iclass+' bwe-h11"><span style="color:#111111;">YEAR </span>'+year+'</td><td class="bwe-td-'+iclass+' bwe-h11"><span style="color:#111111;">Journal Impact</span> '+journalImpact+'</td></tr>');	

 				table.append(tr1);
 				table.append(tr2);
 				c++;
 			});
 			
			$("#loading"+id).remove();
			$("#"+id).append(table);
			
 			
 			},label);

		}
 	}
 
 })(jQuery);
 
